import axios from 'axios'
import { Loading, Message } from 'element-ui'
let instence = axios.create()
instence.defaults.timeout = 5000
let loadinginstace
instence.interceptors.request.use(config => {
    loadinginstace = Loading.service({ fullscreen: true })
    config.headers.Authentication = localStorage.getItem('token') || ''
    return config
}, error => {
    loadinginstace.close()
    Message.error('加载超时')
    return Promise.reject(error)
})
instence.interceptors.response.use(data => {
    loadinginstace.close()
    if (data.status === 200) {
        return data
    }
}, error => {
    loadinginstace.close()
    if (error.response) {
        let msg = error.response.data === null ? '系统内部异常，请联系网站管理员' : error.response.data.message
        switch (error.response.status) {
        case 404:
            Message.error('很抱歉，资源未找到')
            break
        case 403:
        case 401:
            Message.error('很抱歉，您无法访问该资源，可能是因为没有相应权限或者登录已失效')
            break
        default:
            Message.error(msg)
            break
        }
    }
    return Promise.reject(error)
})
export default instence
